package web.controller;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import web.domain.LeaveExample;
import web.domain.User;
import web.domain.UserTask;

import com.njry.util.ActivitiHelper;
import com.njry.util.DBHelper;

public class LoginCheckController implements Controller {
	private final static SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public DBHelper dBHelper;// 
	
/*	@Autowired
	public LeaveMapper leaveDao;// 
	private ActivitiHelper activitiHelper;
	
	public LeaveMapper getLeaveDao() {
		return leaveDao;
	}
	
	public void setLeaveDao(LeaveMapper leaveDao) {
		this.leaveDao = leaveDao;
	}*/
	
	/*public ActivitiHelper getActivitiHelper() {
		return activitiHelper;
	}
	public void setActivitiHelper(ActivitiHelper activitiHelper) {
		this.activitiHelper = activitiHelper;
	}*/
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PrintWriter out=response.getWriter();
		Map model=new HashMap();
		response.setContentType("text/xml;charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		User user = new User();
		
		if(username.equals("1")){
			user.setUser_id("1");
			user.setUser_name("员工");
			request.getSession().setAttribute("userSession",user);
			List list=dBHelper.queryForList("select * from leave_ where user_id='1'");
			for(int i=0;i<list.size();i++){
				System.out.println(list.get(i));
			}
			model.put("list", list);
			LeaveExample leaveExample=new LeaveExample();
			leaveExample.createCriteria().andApplyTimeEqualTo("1");
//			System.out.println("=============="+leaveDao.selectByPrimaryKey("6ec9729a-142f-43fe-89f3-8e2ab2897032").getContent());
			return new ModelAndView("leaveForm/yuangongleaveList",model);
		}else if(username.equals("2")){
			user.setUser_id("2");
			user.setUser_name("经理");
			request.getSession().setAttribute("userSession",user);
			List list=dBHelper.queryForList("select * from leave_ where user_id='2'");
			for(int i=0;i<list.size();i++){
				System.out.println(list.get(i));
			}
			model.put("list", list);
			//获取待办请假单
			/*List<Task> tasks = activitiHelper.taskService.createTaskQuery().taskAssignee(user.getUser_id()).orderByTaskCreateTime().desc().list();
			
			List <UserTask> usertasks=new ArrayList<UserTask>();
			for(Task task : tasks){
//				activitiHelper.taskService.complete(task.getId(), variables);//
				System.out.println(task.getAssignee());
				System.out.println(task.getId());

				String id="";
				UserTask userTask=new UserTask();
				userTask.setTaskName(task.getName());
				userTask.setTaskID(task.getId());
				userTask.setTaskCreateTime(simpleDate.format(task.getCreateTime()));
				userTask.setTaskDescription(task.getDescription());
				
				userTask.setId(id);
				usertasks.add(userTask);
			}
			model.put("usertasks", usertasks);*/
			return new ModelAndView("leaveForm/jinglileaveList",model);
		}else if(username.equals("3")){
			user.setUser_id("3");
			user.setUser_name("老板");
			request.getSession().setAttribute("userSession",user);
//			List<Task> tasks = activitiHelper.taskService.createTaskQuery().taskAssignee(user.getUser_id()).orderByTaskCreateTime().desc().list();
			List <UserTask> usertasks=new ArrayList<UserTask>();
			/*for(Task task : tasks){
				String id="";
				UserTask userTask=new UserTask();
				userTask.setTaskName(task.getName());
				userTask.setTaskID(task.getId());
				userTask.setTaskCreateTime(simpleDate.format(task.getCreateTime()));
				userTask.setTaskDescription(task.getDescription());
				
				userTask.setId(id);
				usertasks.add(userTask);
			}*/
			model.put("usertasks", usertasks);
			return new ModelAndView("leaveForm/laobanleaveList",model);
		}
		return new ModelAndView("index");
	}
	public DBHelper getDBHelper() {
		return dBHelper;
	}
	public void setDBHelper(DBHelper helper) {
		dBHelper = helper;
	}

}
